package com.ljb.calendardemo;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.ljb.calendar.OnPageSelectedListener;
import com.ljb.calendar.bean.DateBean;
import com.ljb.calendar.month.MonthCalendarView;
import com.ljb.calendar.DateViewAdapter;
import com.ljb.calendar.OnDateViewClickListener;
import com.ljb.calendar.month.MonthView;
import com.ljb.calendar.week.WeekCalendarView;
import com.ljb.calendar.week.WeekView;

import org.joda.time.LocalDate;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private WeekCalendarView wcv;
    private MonthCalendarView mcv;
    private TextView tv1, tv2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        intWeekCalendar();
        initMonthCalendar();
    }

    private void intWeekCalendar() {
        wcv = findViewById(R.id.wcv);
        tv1 = findViewById(R.id.tv1);

        wcv.setDateViewAdapter(new DateViewAdapter() {
            @Override
            public View getDateView(View parent, DateBean bean) {
                LocalDate localDate = bean.getLocalDate();
                LocalDate todayDate = bean.getTodayDate();

                TextView textView = new TextView(parent.getContext());
                textView.setBackgroundColor(Color.WHITE);
                textView.setTextColor(Color.BLACK);
                textView.setGravity(Gravity.CENTER);

                if (localDate.isEqual(todayDate)) {
                    textView.setText("今");
                } else {
                    int dayOfMonth = localDate.getDayOfMonth();
                    textView.setText("" + dayOfMonth);
                }
                return textView;
            }
        });

        wcv.setOnDateViewClickListener(new OnDateViewClickListener() {
            @Override
            public void onDateViewClick(View dateView, DateBean bean) {
                Log.e(TAG, "onDateViewClick: " + bean);
                toast(bean.toString());

            }
        });


        wcv.setOnPageSelected(new OnPageSelectedListener() {
            @Override
            public void onPageSelected(View parent, int position) {
                Log.e(TAG, "onPageSelected: " + position);
                WeekView weekView = (WeekView) parent;
                DateBean startDateBean = weekView.getStartDateBean();
                LocalDate localDate = startDateBean.getLocalDate();

                tv1.setText(localDate.toString("yyyy年MM月"));
            }
        });
    }

    private void initMonthCalendar() {
        mcv = findViewById(R.id.mcv);
        tv2 = findViewById(R.id.tv2);

        mcv.setDateViewAdapter(new DateViewAdapter() {
            @Override
            public View getDateView(View parent, DateBean bean) {
                MonthView monthView = (MonthView) parent;

                LocalDate localDate = bean.getLocalDate();
                LocalDate todayDate = bean.getTodayDate();

                TextView textView = new TextView(parent.getContext());
                if (bean.getMonthFlag() == -1) {
                    textView.setTextColor(Color.GRAY);
                } else if (bean.getMonthFlag() == 1) {
                    textView.setTextColor(Color.GRAY);
                } else {
                    textView.setTextColor(Color.BLACK);
                }
                textView.setBackgroundColor(Color.WHITE);
                textView.setGravity(Gravity.CENTER);

                if (localDate.isEqual(todayDate)) {
                    textView.setText("今");
                } else {
                    int dayOfMonth = localDate.getDayOfMonth();
                    textView.setText("" + dayOfMonth);
                }
                return textView;
            }
        });

        mcv.setOnDateViewClickListener(new OnDateViewClickListener() {
            @Override
            public void onDateViewClick(View dateView, DateBean bean) {
                Log.e(TAG, "onDateViewClick: " + bean);
                toast(bean.toString());

                if (bean.getMonthFlag() == -1) {
                    mcv.setCurrentItem(bean.getPositionOfPager() - 1);
                } else if (bean.getMonthFlag() == 1) {
                    mcv.setCurrentItem(bean.getPositionOfPager() + 1);
                }
            }
        });

        mcv.setOnPageSelected(new OnPageSelectedListener() {
            @Override
            public void onPageSelected(View parent, int position) {
                Log.e(TAG, "onPageSelected: " + position);

                MonthView monthView = (MonthView) parent;
                DateBean startDateBean = monthView.getStartDateBean();
                LocalDate localDate = startDateBean.getLocalDate();

                tv2.setText(localDate.toString("yyyy年MM月"));
            }
        });
    }

    public void toast(String content) {
        Toast.makeText(this, content, Toast.LENGTH_SHORT).show();
    }
}
